package com.xgpro.pc.rolemanage.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xgpro.common.enums.ResultCodeEnum;
import com.xgpro.common.exceptions.BusinessRuntimeException;
import com.xgpro.common.results.PageResult;
import com.xgpro.common.results.ResponseResult;
import com.xgpro.domain.rolemanage.entity.Permissions;
import com.xgpro.mapper.rolemanage.PermissionsMapper;
import com.xgpro.pc.rolemanage.service.PermissionsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: xgpro__dev
 * @Description: 权限
 * @Author: wangyang
 * @Date: 2019-09-15 10:09
 **/

@Service
@Transactional(readOnly = false)
public class PermissionsServiceImpl implements PermissionsService {

    @Resource
    private PermissionsMapper permissionsMapper;

    @Override
    public ResponseResult addPermissions(Permissions permissions) {
        int ret = permissionsMapper.insert(permissions);
        if(ret>0){
            return ResponseResult.ok(ret);
        }else {
            return ResponseResult.failure(ResultCodeEnum.ADD_FAILE.getCode(), ResultCodeEnum.ADD_FAILE.getMessage());
        }
    }

    @Override
    public ResponseResult deletePermissions(List<Integer> ids) {

        int ret =-1;
        if (!CollectionUtils.isEmpty(ids)){
            for (Integer id : ids) {
                int cnt = permissionsMapper.queryCountByPermId(id);
                //权限已经被使用，无法删除
                if (cnt>0){
                    throw new BusinessRuntimeException(ResultCodeEnum.CAN_NOT_DELETE_PERMISSIONS.getCode(), ResultCodeEnum.CAN_NOT_DELETE_PERMISSIONS.getMessage());
                }

                //删除对应权限
                ret = permissionsMapper.deleteByPrimaryKey(id);
            }
        }
        return ResponseResult.ok();

    }

    @Override
    public ResponseResult updatePermissions(Permissions permissions) {
        int ret = permissionsMapper.updateByPrimaryKeySelective(permissions);
        if(ret>0){
            return ResponseResult.ok(ret);
        }else {
            return ResponseResult.failure(ResultCodeEnum.ADD_FAILE.getCode(), ResultCodeEnum.ADD_FAILE.getMessage());
        }
    }

    @Override
    public ResponseResult getPermissions(Integer id, Integer pageNum, Integer pageSize,Integer type,String name,Integer pid ) {
        if(id == null){

            Example example = new Example(Permissions.class);
            Example.Criteria criteria = example.createCriteria();
            if (!ObjectUtils.isEmpty(type)){
                criteria.andEqualTo("type",type);
            }
            if (!ObjectUtils.isEmpty(pid)){
                criteria.andEqualTo("pid",pid);
            }

            if (!StringUtils.isEmpty(name)){
                criteria.andLike("permissionsName","%"+name+"%");
            }
            if (pageNum != null && pageSize != null){
                PageHelper.startPage(pageNum, pageSize);
            }
            List<Permissions> list =  permissionsMapper.selectByExample(example);

            return ResponseResult.ok(list);
        }else{
            Permissions permissions = permissionsMapper.selectByPrimaryKey(id);
            return ResponseResult.ok(permissions);
        }
    }

    @Override
    public List<Integer> findPerIdByRoleId(Integer roleId, Integer pid) {
        return permissionsMapper.findPerIdByRoleId(roleId,pid);
    }


}